package org.intellij.lang.assembly.highlighter;

import com.intellij.ide.highlighter.custom.CustomFileHighlighter;
import com.intellij.ide.highlighter.custom.SyntaxTable;
import com.intellij.lexer.LayeredLexer;
import com.intellij.lexer.Lexer;
import com.intellij.lexer.StringLiteralLexer;
import com.intellij.psi.CustomHighlighterTokenType;
import com.intellij.psi.tree.IElementType;
import org.intellij.lang.assembly.parsing.AssemblyLexer;
import org.jetbrains.annotations.NotNull;

/**
 *
 * @author weijiancai
 * @version 1.0
 */
public class AssemblyHighlighter extends CustomFileHighlighter {
    private SyntaxTable myTable;

    public AssemblyHighlighter(SyntaxTable table) {
        super(table);
        this.myTable = table;
    }

    @NotNull
    @Override
    public Lexer getHighlightingLexer() {
        Lexer customFileTypeLexer = new AssemblyLexer(myTable, true);
        if (myTable.isHasStringEscapes()) {
            customFileTypeLexer = new LayeredLexer(customFileTypeLexer);
            ((LayeredLexer)customFileTypeLexer).registerSelfStoppingLayer(new StringLiteralLexer('\"', CustomHighlighterTokenType.STRING,true,"x"),
                    new IElementType[]{CustomHighlighterTokenType.STRING}, IElementType.EMPTY_ARRAY);
            ((LayeredLexer)customFileTypeLexer).registerSelfStoppingLayer(new StringLiteralLexer('\'', CustomHighlighterTokenType.STRING,true,"x"),
                    new IElementType[]{CustomHighlighterTokenType.SINGLE_QUOTED_STRING}, IElementType.EMPTY_ARRAY);
        }
        return customFileTypeLexer;
    }
}
